package cn.edu.henu.team.fragments;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import butterknife.BindView;
import butterknife.OnClick;
import cn.edu.henu.team.R;
import cn.edu.henu.team.adapter.MyFragmentPagerAdapter;
import cn.edu.henu.team.base.BaseActivity;

public class HomeActivity extends BaseActivity {
    @BindView(R.id.viewpager)
    ViewPager2 viewPager;
    @BindView(R.id.id_tab_weixin)
    LinearLayout llChat;
    @BindView(R.id.id_tab_contact)
    LinearLayout llContacts;
    @BindView(R.id.id_tab_find)
    LinearLayout llFind;
    @BindView(R.id.id_tab_profile)
    LinearLayout llProfile;
    @BindView(R.id.tab_iv_weixin)
    ImageView ivChat;
    @BindView(R.id.tab_iv_contact)
    ImageView ivContacts;
    @BindView(R.id.tab_iv_find)
    ImageView ivFind;
    @BindView(R.id.tab_iv_profile)
    ImageView ivProfile;
    ImageView ivCurrent;

    @Override
    protected int initLayout() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(fragment2.newInstance());
        fragments.add(fragment1.newInstance());
        fragments.add(fragment3.newInstance());
        fragments.add(fragment4.newInstance());
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), getLifecycle(), fragments);
        viewPager.setOffscreenPageLimit(fragments.size()); //预加载
        viewPager.setUserInputEnabled(false);
        viewPager.setAdapter(pagerAdapter);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    @Override
    protected void initData() {
        ivContacts.setSelected(true);
        ivCurrent = ivContacts;
    }

    /**
     * 监听点击事件
     *
     * @param view
     */
    @OnClick({R.id.id_tab_weixin, R.id.id_tab_contact, R.id.id_tab_find, R.id.id_tab_profile})
    public void onClick(View view) {
        changeTab(view.getId());
    }

    //进行碎片切换
    private void changeTab(int position) {
        ivCurrent.setSelected(false);
        switch (position) {
            case R.id.id_tab_weixin:
                viewPager.setCurrentItem(1);
            case 1:
                ivChat.setSelected(true);
                ivCurrent = ivChat;
                break;
            case R.id.id_tab_contact:
                viewPager.setCurrentItem(0);
            case 0:
                ivContacts.setSelected(true);
                ivCurrent = ivContacts;
                break;
            case R.id.id_tab_find:
                viewPager.setCurrentItem(2);
            case 2:
                ivFind.setSelected(true);
                ivCurrent = ivFind;
                break;
            case R.id.id_tab_profile:
                viewPager.setCurrentItem(3);
            case 3:
                ivProfile.setSelected(true);
                ivCurrent = ivProfile;
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {   //进行碎片的跳转
        int id = getIntent().getIntExtra("id", 0);
        switch (id) {
            case 1:
                viewPager.setCurrentItem(id);
                break;
            case 2:
                viewPager.setCurrentItem(id);
                break;
            case 3:
                viewPager.setCurrentItem(id);
                break;
            case 4:
                viewPager.setCurrentItem(id);
                break;
            default:
                break;
        }
        super.onResume();
    }
}